<template>
  <div class="main-container">
    <el-form :model="query" class="form-right" inline label-width="80px">
      <el-form-item>
        <el-input v-model="query.questionTitle" placeholder="根据题目名称搜索" prefix-icon="Search" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery()"> 查询</el-button>
        <el-button @click="resetQuery()">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="page.loading" :data="page.list">
      <el-table-column label="题型" min-width="20">
        <template #default="scope">
          <enum-view :enum-name="'QuestionType'" :enum-value="scope.row.questionType" />
        </template>
      </el-table-column>
      <el-table-column label="难度等级" min-width="20">
        <template #default="scope">
          <enum-view :enum-name="'DifficultyLevel'" :enum-value="scope.row.difficultyLevel" />
        </template>
      </el-table-column>
      <el-table-column label="题目">
        <template #default="scope">
          <span v-html="scope.row.questionTitle" />
        </template>
      </el-table-column>
      <el-table-column label="正确率" min-width="20" prop="correctRate">
        <template #default="scope">
          <el-button link type="primary" @click="onView(scope.row, true)">{{ scope.row.correctRate }}%</el-button>
        </template>
      </el-table-column>
      <el-table-column label="错误率" min-width="20" prop="errorRate">
        <template #default="scope">
          <el-button link type="primary" @click="onView(scope.row, false)">{{ scope.row.errorRate }}%</el-button>
        </template>
      </el-table-column>
    </el-table>
    <common-pagination v-model:page="page" @pagination="handlePage" />
  </div>
  <exam-analysis-detail v-if="visible" :analysis-detail="analysisDetail" :title="title" :visible="visible" @close="visible = false" />
</template>
<script setup>
  import useTable from '@/utils/table'
  import { examApi } from '@/api/exam'
  import CommonPagination from '@/components/Common/Pagination/index.vue'
  import EnumView from '@/components/Enum/View/index.vue'
  import ExamAnalysisDetail from '@/views/exam/record/ExamAnalysisDetail.vue'
  import { reactive, ref } from 'vue'

  const props = defineProps({
    examType: { type: Number, default: 0 },
    examId: { type: String, default: '' },
    periodId: { type: String, default: '' }
  })

  // 查看详情
  const visible = ref(false)
  const title = ref('')
  const analysisDetail = reactive({})
  const onView = (row, correct) => {
    analysisDetail.examType = props.examType
    analysisDetail.relateId = props.examId
    analysisDetail.questionId = row.questionId
    analysisDetail.correct = correct
    title.value = correct ? '正确率详情' : '错误率详情'
    visible.value = true
  }

  // 基础功能
  const { page, handlePage, query, handleQuery, resetQuery } = useTable(
    {
      page: examApi.examAnalysis
    },
    { examType: props.examType, relateId: props.examType === 3 ? props.periodId : props.examId }
  )
</script>
